﻿using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ScannerApp.ScannerWebserviceWrapper;
using ScannerApp.Utils;
using System.Collections;
using CodeBetter.Json;

namespace ScannerApp.Forms
{
    public partial class ScannerBaseForm : BaseForm
    {
        protected String mOrderCode = string.Empty;
        protected IScanProduct mScanProductOrder;
        protected ScanBarcodeStatus mCurrentStatus;
        protected ProductBarcodeType mCurrentProductBarcodeType = ProductBarcodeType.Unknown;
        protected String mCurrentShelfCode = string.Empty;
        protected String mCurrentProductCode = string.Empty;
        protected String mCurrentProductLot = string.Empty;
        protected String mCurrentProductValidity = string.Empty;
        protected String mCurrentProductQR = string.Empty;
        protected float mCurrentProductQuantity = 0;

        protected System.Windows.Forms.Label lblProductInfo;
        protected System.Windows.Forms.Control txtScanCode;
        protected System.Windows.Forms.Label lblMessage;
        protected System.Windows.Forms.Label lblHeaderInfo;

        public ScannerBaseForm()
        {
            InitializeComponent();
            
        }
 
        public virtual void Init()
        {
            AssignControls();
            AdjustControlsSize();
        }

        public virtual void AssignControls()
        {

        }

        protected virtual void AdjustControlsSize()
        {
            this.Width = Screen.PrimaryScreen.WorkingArea.Width;
            foreach (Control control in this.Controls)
            {
                control.Width = Screen.PrimaryScreen.WorkingArea.Width;
            }
        }

        public override void HandleBarCode(string BarCode)
        {
            base.HandleBarCode(BarCode);
            DisplayScannedCode(BarCode);
        }


        protected virtual void DispalyMessage(String message)
        {
            lblMessage.Text = message;
            DispalyProductMessage();
            DisplayHeaderInfo();
        }

        protected void DisplayHeaderInfo()
        {
            //lblHeaderInfo.Text = (mScanProductOrder == null) ? String.Empty : mScanProductOrder.GetHeaderInfo();
        }

        protected virtual void DisplayScannedCode(string BarCode)
        {
            lblHeaderInfo.Text = "扫描到:"+BarCode;
        }

        protected virtual void DispalyProductMessage()
        {
            String result = this.mCurrentProductCode.Equals(string.Empty) ? string.Empty : string.Format("产品代码:{0},", this.mCurrentProductCode);
            result += this.mCurrentProductLot.Equals(string.Empty) ? string.Empty : string.Format("批号:{0},", this.mCurrentProductLot);
            result += this.mCurrentProductValidity.Equals(string.Empty) ? string.Empty : string.Format("效期:{0},", this.mCurrentProductValidity);
            result += this.mCurrentProductQR.Equals(string.Empty) ? string.Empty : string.Format("QR:{0},", this.mCurrentProductQR);
            result += this.mCurrentShelfCode.Equals(string.Empty) ? string.Empty : string.Format("货位号:{0}", this.mCurrentShelfCode);

            this.lblProductInfo.Text = result;
        }

        protected void SetProductMessage(String productCode, String lot, String QR)
        {
            mCurrentProductCode = productCode;
            mCurrentProductLot = lot;
            mCurrentProductQR = QR;
        }

        protected void SetProductMessage(String productCode, String lot, String QR ,String shelfCode)
        {
            SetProductMessage(productCode, lot, QR);
            this.mCurrentShelfCode = shelfCode;
        }

        protected void ClearProductValues()
        {
            this.mCurrentProductCode = string.Empty;
            mCurrentProductLot = String.Empty;
            mCurrentProductQR = String.Empty;
            mCurrentProductValidity = string.Empty;
            mCurrentProductQuantity = 0;
        }

        protected void ClearFirstLayerValues()
        {
            mCurrentProductLot = String.Empty;
            mCurrentProductQR = String.Empty;
            mCurrentProductValidity = string.Empty;
            mCurrentProductQuantity = 0;
        }

        protected void ClearSecondLayerValues()
        {
            mCurrentProductQR = String.Empty;
            mCurrentProductQuantity = 0;
        }



        protected virtual void ScanProductWithQuantity(string productCode, string lot, float quantity)
        {

        }

        protected void ScanProductCode(string barCode, IScanProduct iOrder)
        {
            //check the product code first
            Hashtable result = Utils.Utils.CheckBarcodeType(barCode);

            //if the order enables qr, all products of it will need QR only.
            if (result[CheckBarcodeKeys.BarcodeType].Equals(ProductBarcodeType.QR))
            {
                if (this.mCurrentProductCode == string.Empty && !iOrder.IsQRInOrder(barCode))
                {
                    DispalyMessage("序列号不属于此单号.");
                    return;
                }

                ScanQRCode(barCode);
            }
            else
            {
                if (this.mCurrentProductBarcodeType.Equals(ProductBarcodeType.Unknown) || this.mCurrentProductBarcodeType.Equals(ProductBarcodeType.QR))
                {
                    if (result[CheckBarcodeKeys.BarcodeType].Equals(ProductBarcodeType.HIBC_Second)
                        || result[CheckBarcodeKeys.BarcodeType].Equals(ProductBarcodeType.GS1_Second)
                        //|| result[CheckBarcodeKeys.BarcodeType].Equals(ProductBarcodeType.QR)
                        )
                    {
                        DispalyMessage("请扫描产品一级码.");
                        return;
                    }
                }
                if (this.mCurrentProductBarcodeType.Equals(ProductBarcodeType.HIBC_First))
                {
                    if (!result[CheckBarcodeKeys.BarcodeType].Equals(ProductBarcodeType.HIBC_Second))
                    {
                        DispalyMessage("请扫描HIBC二级码.");
                        return;
                    }
                }
                if (this.mCurrentProductBarcodeType.Equals(ProductBarcodeType.HIBC_Second))
                {
                    if (!result[CheckBarcodeKeys.BarcodeType].Equals(ProductBarcodeType.QR))
                    {
                        DispalyMessage("请扫描序列号.");
                        return;
                    }
                }

                if (this.mCurrentProductBarcodeType.Equals(ProductBarcodeType.GS1_First))
                {
                    if (!result[CheckBarcodeKeys.BarcodeType].Equals(ProductBarcodeType.GS1_Second))
                    {
                        DispalyMessage("请扫描GS1二级码.");
                        return;
                    }
                }
                if (this.mCurrentProductBarcodeType.Equals(ProductBarcodeType.GS1_Second))
                {
                    if (!result[CheckBarcodeKeys.BarcodeType].Equals(ProductBarcodeType.QR))
                    {
                        DispalyMessage("请扫描产品的序列号");
                        return;
                    }
                }


                if (result[CheckBarcodeKeys.BarcodeType].Equals(ProductBarcodeType.HIBC_First)
                    || result[CheckBarcodeKeys.BarcodeType].Equals(ProductBarcodeType.GS1_First)
                    )
                {
                    //对于移库，如果产品码已开启QR,这时候不允许扫描产品码。
                    if (!iOrder.IsCanScanProductCode(result[CheckBarcodeKeys.ProductCode].ToString()))
                    {
                        DispalyMessage("此产品已开启序列号管理,请直接扫描序列号.");
                        this.mCurrentProductBarcodeType = ProductBarcodeType.QR;
                        return;
                    }

                    //判断产品码是否在产品列表里
                    if (!iOrder.IsProductCodeInOrder(result[CheckBarcodeKeys.ProductCode].ToString()))
                    {
                        DispalyMessage("此产品码不在产品列表中.");
                        return;
                    }

                    DispalyMessage("已扫描到一级码，请扫描二级码.");

                    this.SetProductMessage(result[CheckBarcodeKeys.ProductCode].ToString(), string.Empty, string.Empty);
                    this.ClearFirstLayerValues();
                    this.DispalyProductMessage();
                }
                else if (result[CheckBarcodeKeys.BarcodeType].Equals(ProductBarcodeType.HIBC_Second)
                    || result[CheckBarcodeKeys.BarcodeType].Equals(ProductBarcodeType.GS1_Second)
                    || result[CheckBarcodeKeys.BarcodeType].Equals(ProductBarcodeType.GS1_Complete)
                    )
                {
   
                    if(result[CheckBarcodeKeys.BarcodeType].Equals(ProductBarcodeType.GS1_Complete))
                    {
                        //对于移库，如果产品码已开启QR,这时候不允许扫描产品码。
                        if (!iOrder.IsCanScanProductCode(result[CheckBarcodeKeys.ProductCode].ToString()))
                        {
                            DispalyMessage("此产品已开启序列号管理,请直接扫描序列号.");
                            this.mCurrentProductBarcodeType = ProductBarcodeType.QR;
                            return;
                        }

                        this.SetProductMessage(result[CheckBarcodeKeys.ProductCode].ToString(), result[CheckBarcodeKeys.Lot].ToString(), string.Empty);
                    }
                    //批号不属于此订单
                    if (!iOrder.IfLotInOrder(this.mCurrentProductCode,result[CheckBarcodeKeys.Lot].ToString()))
                    {
                        DispalyMessage("扫描到的批号不属于此订单");
                        return;
                    }
                    
                    this.mCurrentProductLot = result[CheckBarcodeKeys.Lot].ToString();
                    if(result[CheckBarcodeKeys.InvalidDate]!=null)
                    {
                        this.mCurrentProductValidity = result[CheckBarcodeKeys.InvalidDate].ToString();
                    }

                    this.ClearSecondLayerValues();

                    //the product doesn't enable QR.
                    if (!iOrder.IsProductEnablingQR(this.mCurrentProductCode))
                    {
                        //pop up a windows to accept the number of the product.
                        QuantityPopupForm form = new QuantityPopupForm();
                        form.ShowDialog();
                        mCurrentProductQuantity = form.Quantity;

                        //if it doesn't need to scan shelf after fill quantity, save data to server dierectly.
                        if (!iOrder.IsScanShelfAfterQuantity())
                        {                          
                            ScanProductWithQuantity(this.mCurrentProductCode, this.mCurrentProductLot, mCurrentProductQuantity);
                        }
                        else
                        {
                            this.mCurrentStatus = ScanBarcodeStatus.ShelfCode;
                            DispalyMessage("请扫描货位号");
                        }
                        return;
                    }
                    else
                    {
                        if (iOrder.IsScanQRAfterProduct())
                        {
                            if (!result[CheckBarcodeKeys.BarcodeType].Equals(ProductBarcodeType.GS1_Complete))
                            {
                                DispalyMessage("已扫描二级码,请继续扫描序列号.");
                            }
                            else
                            {
                                DispalyMessage("已检测到GS1码,请继续扫描序列号");
                            }
                        }
                        else
                        {
                            DispalyMessage("此产品已开启序列号管理，请扫描序列号");
                        }
                    }
                }
              
            }
            this.mCurrentProductBarcodeType = (ProductBarcodeType)result[CheckBarcodeKeys.BarcodeType];
        }

        protected virtual void ScanQRCode(string QRcode)
        {
        }


        protected bool IsReturnKey(KeyPressEventArgs e)
        {
            if (e.KeyChar.Equals((char)13))
            {
                return true;
            }
            return false;
        }

        protected void HandleKeyPress(KeyPressEventArgs e)
        {
            if (IsReturnKey(e))
            {
                HandleBarCode(this.txtScanCode.Text);
            }
        }

        protected bool IsShelfcode(String barcode, long warehouseId)
        {
            InStockPutOnShelfParameter param = new InStockPutOnShelfParameter();
            param.ShelfCode = barcode;
            // 扫描入库时，货位必须是订单指定仓库的货位
            if (warehouseId != -1)
            {
                param.WarehouseId = warehouseId;
            }
            string strResult = ScannerWebServiceProxy.Instance.inStockCheckShelf(Converter.Serialize(param));
            BaseResult<Object> result = Converter.Deserialize<BaseResult<Object>>(strResult);
            if (result.IsSucessful())
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}